﻿using System;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
using SyCODE.Component;
using SyCODE.Component.DataFactory;
using System.Data.Common;
using System.Collections.Specialized;
using SyCODE.Component.Cryptography;
using SyCODE.Component.Security;
using ICSharpCode.SharpZipLib.Zip;

namespace SyCODE.Blog.Admin
{
    /// <summary>
    /// DeleteTopics.aspx 的处理程序
    /// </summary>
    public class DeleteTopics : HandlerBase
    {
        public override void ProcessRequest(HttpContext context)
        {
            if (this.Authenticated)
            {
                NameValueCollection nvc = new NameValueCollection(context.Request.Form);
                string[] topics = nvc["ids"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string[] authors = nvc["authors"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (topics.Length > 0 && topics.Length == authors.Length)
                {
                    string reason = String.Format(StringResource.GetField("topicdeletereason"), nvc["reason"]);
                    string topicbackup = StringResource.GetField("topicbackup");
                    string[] backs = new string[topics.Length];

                    Random random = new Random();
                    
                    for (int i = 0; i < topics.Length; i++)
                    {
                        SearchEngine.Simple.Delete(topics[i]);

                        string userDir = AppPath.GetUserPhysicalPath(authors[i]);
                        string topicDir = Path.Combine(userDir, String.Format("topic/{0}/", topics[i]));

                        string topic = Path.Combine(topicDir, "main.xml");

                        if (File.Exists(topic))
                        {
                            XmlDocument topicDom = new XmlDocument();
                            topicDom.Load(topic);

                            XmlNode bodyNode = topicDom.DocumentElement.SelectSingleNode("body");
                            string body = bodyNode.InnerText;
                            string description = String.Empty;

                            XmlNode desNode = topicDom.DocumentElement.SelectSingleNode("head/meta[@name='description']");
                            if (desNode != null)
                            {
                                description = desNode.Attributes["content"].Value;
                                desNode.Attributes["content"].Value = reason;
                            }
                            string title = topicDom.DocumentElement.SelectSingleNode("head/title").InnerText;
                            string keywords = topicDom.DocumentElement.SelectSingleNode("head/meta[@name='keywords']").Attributes["content"].Value;

                            bodyNode.InnerText = reason;
                            topicDom.Save(topic);

                            byte[] buffer = Encoding.UTF8.GetBytes(String.Format(topicbackup, title, keywords, description, body));

                            ZipOutputStream zipFile = new ZipOutputStream(File.Create(Path.Combine(topicDir, backs[i] = (0xBAC00000 + random.Next(0xFFFFF)).ToString("X") + ".zip")));
                            zipFile.PutNextEntry(new ZipEntry("back.html"));
                            zipFile.Write(buffer, 0, buffer.Length);
                            zipFile.Close();
                        }
                    }

                    nvc.Add("backs", String.Join(",", backs));
                    nvc.Set("ids", String.Join(",", topics));
                    nvc.Set("authors", String.Join(",", authors));

                    DBAM dbam = new DBAM();
                    dbam.BuildParameters("deletetopics", nvc);

                    try
                    {
                        dbam.Execute();
                    }
                    finally
                    {
                        dbam.Dispose();
                    }
                }
            }
        }
    }
}
